﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    /// <summary>
    /// 课程分数，事务逻辑
    /// </summary>
    public class ScoreBS
    {
        /// <summary>
        /// 获取单个课程成绩
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public int GetScore(string course_id, string student_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var score = db.score.FirstOrDefault(x => x.course_id == course_id && x.student_id == student_id);
                if (score == null) return 0;    //!!
                return score.score_exam;
            }
        }

        /// <summary>
        /// 获取学生的成绩列表
        /// </summary>
        /// <param name="student_id"></param>
        /// <returns></returns>
        static public List<Score> GetScoreList(string student_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var ret = db.score.Where(x => x.student_id == student_id);
                List<Score> list = new List<Score>();
                foreach (var score in ret)
                {
                    Score model = new Score();
                    model.course_id = score.course_id;
                    model.score_exam = score.score_exam;
                    model.student_id = score.student_id;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 添加分数
        /// </summary>
        /// <returns>成功返回true</returns>
        static public bool AddScore(string student_id, string course_id, int score)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    db.score.AddObject(new score()
                    {
                        student_id = student_id,
                        course_id = course_id,
                        score_exam = score
                    });
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 添加分数
        /// </summary>
        /// <param name="model"></param>
        /// <returns>成功返回true</returns>
        static public bool AddScore(Score model)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    db.score.AddObject(new score()
                    {
                        student_id = model.student_id,
                        course_id = model.course_id,
                        score_exam = model.score_exam
                    });
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
